setwd("~/Desktop/working-with-lyle/Formality_Project")#set our WD
if (!require("pacman")) install.packages("pacman") #run this if you don't have pacman
library(pacman)
pacman::p_load(tidyverse,rlang, zoo, lubridate, plotrix, ggpubr, caret, broom, kableExtra, reactable, install = T)
#use pacman to load packages quickly palette_map = c("#3B9AB2", "#EBCC2A", "#F21A00")
palette_condition = c("#ee9b00", "#bb3e03", "#005f73")
plot_aes = theme_classic() +
theme(legend.position = "top",
legend.text = element_text(size = 12),
text = element_text(size = 16, family = "Futura Medium"),
axis.text = element_text(color = "black"),
axis.line = element_line(colour = "black"),
axis.ticks.y = element_blank()) table_model = function(model_data) {
model_data %>%
tidy() %>%
rename("SE" = std.error,
"t" = statistic,
"p" = p.value) %>%
kable() %>%
kableExtra::kable_styling()
}df <- read_csv('Atlantic_Cleaned_all_vars.csv') #read in the data tidy_df <- df %>%
group_by(Date) %>% ###grouping by the year
mutate_at(vars("Analytic","WPS","BigWords","Period","readability","grade_level"), as.numeric) %>%
summarise_at(vars("Analytic","WPS","BigWords","Period","readability","grade_level"), funs(mean, std.error),)
# Get the mean values for the year 1932
year_means <- tidy_df %>%
filter(Date == 1857)
#create centered variables on 1857
tidy_df$Analytic_centered <- tidy_df$Analytic_mean - 85.94
tidy_df$WPS_centered <- tidy_df$WPS_mean - 37.14
tidy_df$BigWords_centered <- tidy_df$BigWords_mean - 25.68
tidy_df$Period_centered <- tidy_df$Period_mean - 4.589
tidy_df$readability_centered <- tidy_df$readability_mean - 57.45
tidy_df$grade_level_centered <- tidy_df$grade_level_mean - 10.71df %>%
select(Date) %>%
range()## [1] 1857 2022
df %>%
select(Filename) %>%
dplyr::summarize(n = n()) %>%
reactable::reactable(striped = TRUE)Plotting each individual book along with the line of best fit (loess regression) to see trends in the data.
#Plot our smoothed data
#we are using Non-tidy data here to capture the individual variation
#Analytic Thinking
Analytic_smooth <- ggplot(data=df, aes(x=Date, y=Analytic, group=1)) +
ggtitle("Analytic Thinking") +
geom_point(color = "dodgerblue3", alpha = 0.15) +
geom_smooth(method = "loess", span = 0.50 )+
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=25,label="
estimate = 0.1
p-value <. 001
", size = 3.5)
#Bigwords
Bw_smooth <- ggplot(data=df, aes(x=Date, y=BigWords, group=1)) +
ggtitle("Big Words (Letters > 6)") +
geom_point(color = "dodgerblue3", alpha = 0.15) +
geom_smooth(method = "loess", span = 0.50 )+
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=40,label="
estimate = 0.0701
p-value < .001
", size = 3.5)
#Periods
period_smooth <- ggplot(data=df, aes(x=Date, y=Period, group=1)) +
ggtitle("Period Usage") +
geom_point(color = "dodgerblue3", alpha = 0.15) +
geom_smooth(method = "loess", span = 0.50 )+
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=15,label="
estimate = 0.0204
p-value < .001
", size = 3.5)
#words per sentence
wps_smooth <- ggplot(data=df, aes(x=Date, y=WPS, group=1)) +
ggtitle("Words per Sentence") +
geom_point(color = "dodgerblue3", alpha = 0.15) +
geom_smooth(method = "loess", span = 0.50 )+
plot_aes +
labs(x = "Year", y = '# of Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=200,label="
estimate = -0.0497
p-value < .001
", size = 3.5)
smooth_graphs <- ggpubr::ggarrange(Analytic_smooth,Bw_smooth,period_smooth,wps_smooth,
ncol=2, nrow=2, common.legend = TRUE, legend = "bottom")
annotate_figure(smooth_graphs,
top = text_grob("Smooth Formality Graphs", color = "black", face = "bold", size = 20),
bottom = text_grob(
"Note. Horizontal shading represents Standard Error."
, color = "Black",
hjust = 1.05, x = 1, face = "italic", size = 14))Analytic_smooth_tidy <- ggplot(data=tidy_df, aes(x=Date, y=Analytic_mean, group=1)) +
ggtitle("Analytic Thinking") +
geom_point(color = "dodgerblue3", alpha = 0.7) +
geom_smooth(method = "loess", span = 0.50 )+
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=95,label="
estimate = 0.0782
p-value < .001
", size = 3.5)
#Bigwords
Bw_smooth_tidy <- ggplot(data=tidy_df, aes(x=Date, y=BigWords_mean, group=1)) +
ggtitle("Six letter > N words") +
geom_point(color = "dodgerblue3", alpha = 0.7) +
geom_smooth(method = "loess", span = 0.90 )+
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=30,label="
estimate = 0.0431
p-value < .001
", size = 3.5)
#Periods
period_smooth_tidy <- ggplot(data=tidy_df, aes(x=Date, y=Period_mean, group=1)) +
ggtitle("Period Usage") +
geom_point(color = "dodgerblue3", alpha = 0.7) +
geom_smooth(method = "loess", span = 0.50 )+
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=7,label="
estimate = 0.0128
p-value < .001
", size = 3.5)
#words per sentence
wps_smooth_tidy <- ggplot(data=tidy_df, aes(x=Date, y=WPS_mean, group=1)) +
ggtitle("Words per Sentence") +
geom_point(color = "dodgerblue3", alpha = 0.7) +
geom_smooth(method = "loess", span = 0.70 )+
plot_aes +
labs(x = "Year", y = '# of Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold")) +
annotate(geom="text",x=1860,
y=65,label="
estimate = -0.014
p-value < .001
", size = 3.5)
tidy_smooth_graphs <- ggpubr::ggarrange(Analytic_smooth_tidy,Bw_smooth_tidy,
period_smooth_tidy,wps_smooth_tidy,
ncol=2, nrow=2, common.legend = TRUE, legend = "bottom")
annotate_figure(tidy_smooth_graphs,
top = text_grob("Smooth Formality Graphs (grouped by year)", color = "black", face = "bold", size = 20),
bottom = text_grob(
"Note. Horizontal shading represents Standard Error.
Estimates are from centered analyses (centered on means from 1857)"
, color = "Black",
hjust = 1.05, x = 1, face = "italic", size = 16))Plotting the data by year (one data point per year).
Analytic <- ggplot(data=tidy_df, aes(x=Date, y=Analytic_mean, group=1)) +
geom_line(colour = "dodgerblue3") +
geom_ribbon(aes(ymin=Analytic_mean-Analytic_std.error, ymax=Analytic_mean+Analytic_std.error), alpha=0.2) +
ggtitle("Analytic Thinking") +
plot_aes +
labs(x = "Year", y = 'Standardized score') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold"))
#WPS
WPS <- ggplot(data=tidy_df, aes(x=Date, y=WPS_mean, group=1)) +
geom_line(colour = "dodgerblue3") +
geom_ribbon(aes(ymin=WPS_mean-WPS_std.error, ymax=WPS_mean+WPS_std.error), alpha=0.2) +
ggtitle("WPS") +
plot_aes +
labs(x = "Year", y = '# of Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold"))
#BigWords
BigWords <- ggplot(data=tidy_df, aes(x=Date, y=BigWords_mean, group=1)) +
geom_line(colour = "dodgerblue3") +
geom_ribbon(aes(ymin=BigWords_mean-BigWords_std.error, ymax=BigWords_mean+BigWords_std.error), alpha=0.2) +
ggtitle("Big Words N > 6") +
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold"))
#period frequency
Period <- ggplot(data=tidy_df, aes(x=Date, y=Period_mean, group=1)) +
geom_line(colour = "dodgerblue3") +
geom_ribbon(aes(ymin=Period_mean-Period_std.error, ymax=Period_mean+Period_std.error), alpha=0.2) +
ggtitle("Period-usage") +
plot_aes +
labs(x = "Year", y = '% of Total Words') +
theme(axis.text.x=element_text(angle=45, hjust=1),
plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 16)) +
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))+
theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5, face = "bold", size = 20)) +
theme(axis.text=element_text(size = 14),
axis.title=element_text(size = 20,face="bold"))
#raw graphs
raw_graphs <- ggpubr::ggarrange(Analytic,BigWords,Period,WPS,ncol=2, nrow=2, common.legend = TRUE, legend = "bottom")
annotate_figure(raw_graphs,
top = text_grob("Raw Formality Graphs (grouped by year)", color = "black", face = "bold", size = 20),
bottom = text_grob("Note. Horizontal shading represents Standard Error.
Graphs are of books in the collection"
, color = "Black",
hjust = 1.05, x = 1, face = "italic", size = 16))Models presented in order: Raw data, aggregated by year, centered on 1857
#Raw Data
AT_RAW <- lm(Analytic ~ Date, data = df)
#Tidy Data
AT_TIDY <- lm(Analytic_mean ~ Date, data = tidy_df)
#centered
AT_centered <- lm(Analytic_centered ~ Date, data = tidy_df)
table_model(AT_RAW)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -114.2 | 12.7358 | -8.965 | 0 |
| Date | 0.1 | 0.0065 | 15.333 | 0 |
table_model(AT_TIDY)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -73.5447 | 21.2185 | -3.466 | 7e-04 |
| Date | 0.0782 | 0.0109 | 7.152 | 0e+00 |
table_model(AT_centered)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -159.4847 | 21.2185 | -7.516 | 0 |
| Date | 0.0782 | 0.0109 | 7.152 | 0 |
BW_Raw <- lm(BigWords ~ Date, data = df)
BW_Tidy <- lm(BigWords_mean ~ Date, data = tidy_df)
BW_centered <- lm(BigWords_centered ~ Date, data = tidy_df)
table_model(BW_Raw)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -114.0403 | 4.2057 | -27.12 | 0 |
| Date | 0.0701 | 0.0022 | 32.55 | 0 |
table_model(BW_Tidy)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -62.9412 | 9.753 | -6.454 | 0 |
| Date | 0.0431 | 0.005 | 8.569 | 0 |
table_model(BW_centered)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -88.6212 | 9.753 | -9.087 | 0 |
| Date | 0.0431 | 0.005 | 8.569 | 0 |
#Periods
Period_Raw <- lm(Period ~ Date, data = df)
Period_Tidy <- lm(Period_mean ~ Date, data = tidy_df)
Period_centered <- lm(Period_centered ~ Date, data = tidy_df)
table_model(Period_Raw)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -34.4258 | 1.5638 | -22.01 | 0 |
| Date | 0.0204 | 0.0008 | 25.49 | 0 |
table_model(Period_Tidy)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -20.0945 | 3.3420 | -6.013 | 0 |
| Date | 0.0128 | 0.0017 | 7.416 | 0 |
table_model(Period_centered)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | -24.6835 | 3.3420 | -7.386 | 0 |
| Date | 0.0128 | 0.0017 | 7.416 | 0 |
#WPS
WPS_Raw <- lm(WPS ~ Date, data = df)
WPS_Tidy <- lm(WPS_mean ~ Date, data = tidy_df)
WPS_centered <- lm(WPS_centered ~ Date, data = tidy_df)
table_model(WPS_Raw)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | 125.9095 | 14.2534 | 8.834 | 0 |
| Date | -0.0497 | 0.0073 | -6.805 | 0 |
table_model(WPS_Tidy)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | 58.434 | 28.4925 | 2.0508 | 0.0419 |
| Date | -0.014 | 0.0147 | -0.9535 | 0.3417 |
table_model(WPS_centered)| term | estimate | SE | t | p |
|---|---|---|---|---|
| (Intercept) | 21.294 | 28.4925 | 0.7473 | 0.4559 |
| Date | -0.014 | 0.0147 | -0.9535 | 0.3417 |